<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        #btn {
            position: fixed;
            z-index: 1000;
            width: 200px;
            height: 50px;
            top: 20px;
            right: 50px;
        }
    </style>
</head>

<body>
    <button id="btn"  onclick="clearMap()">清除数据</button>
    <div id="map">
    </div>
    <script>
        var map = new ol.Map({
            target: "map",
            layers: [gaode],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })
        /* 1、点击事件创建要素 */
        /* 2、将要素添加到空图层的矢量数据源中 */

        /* 创建一个空图层 */
        let style = new ol.style.Style({
            //形状
            image: new ol.style.Circle({
                /* radius 设置半径 */
                radius: 30,
                fill: new ol.style.Fill({
                    color: '#ffcc33'
                }),
                stroke: new ol.style.Stroke({
                    color: "#333",
                    width: 5
                })
            })
        })
        let source = new ol.source.Vector({})
        let layer = new ol.layer.Vector({
            source,
            style
        })
        map.addLayer(layer);

        /* 点击事件设置要素 */
        map.on("click", evt => {
            let point = new ol.Feature({
                geometry: new ol.geom.Point(evt.coordinate)
            })
            // source.addFeature(point)
            source.addFeatures([point])
        })

        function clearMap(){
            console.log(source)
            source.clear()
        }
    </script>
</body>

</html>